import { getBaseUrl } from '@/services/urlconst';
import { requestFunComponFiex } from '@/utils';
import { useCallback, useMemo } from 'react';
import { useRequest } from 'umi';
import { WorkflowSwitchAPI } from '../common/type';
import { getSearchList } from '../mconfig';

let baseUrl = getBaseUrl();

const procdefDataGridServices = (
  procdefDataGridCallback: (res: any) => void,
  controller: string,
) => {
  const { tableProps, data, loading, run, params } = useRequest(
    (opt: WorkflowSwitchAPI.IManageSwitchServices) => {
      let requestURL = `${baseUrl}/${controller}/procdefDataGrid`;
      let personBody: any = {
        pageSize: 10,
        current: 1,
        sort: 'deployTime',
        order: 'desc',
        ...opt,
      };
      return requestFunComponFiex('POST', requestURL, personBody);
    },
    {
      manual: true, //页面初始请求为 true
      paginated: true,
      defaultPageSize: 10,
      onSuccess: procdefDataGridCallback,
    },
  );

  /* 获取页面数据 */
  const getProcdefData = useCallback(
    (otherRequest: WorkflowSwitchAPI.IGridRequestType | any) => {
      const request: WorkflowSwitchAPI.IGridRequestType | any = {
        ...otherRequest,
      };

      run({
        ...request,
      });
    },
    [],
  );

  /* 页面列表 */
  let procdefProps: any = useMemo(() => {
    return {
      pageData: data,
      pagination: tableProps?.pagination,
    };
  }, [data, tableProps]);

  const searchList = useMemo(() => {
    return {
      searchList: getSearchList(),
    };
  }, []);

  return {
    getProcdefData,
    pageLoading: loading,
    ...searchList,
    ...procdefProps,
  };
};

export { procdefDataGridServices };
